package cn.school.servlet;

import cn.school.entity.Student;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@WebServlet("/uploadServlet")
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//        req.setCharacterEncoding("utf-8");
//        String name=req.getParameter("sname");
//        String qq=req.getParameter("qq");
//        String wec=req.getParameter("wechat");
//        String file=req.getParameter("idcard");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print("<html><body>");
        DiskFileItemFactory df = new DiskFileItemFactory();//文件暂存的位置。。。
        ServletFileUpload upload = new ServletFileUpload(df);
        upload.setHeaderEncoding("utf-8");
        Student student = new Student();
        try {
            List<FileItem> fileItems = upload.parseRequest(req);
            for (FileItem fi : fileItems) {
                //
                if (fi.isFormField()) {//普通的表单域
                    writer.print(fi.getFieldName() + "=" + fi.getString("utf-8") + "<br/>");
                    switch (fi.getFieldName()) {
                        case "sname":
                            student.setSname(fi.getString("utf-8"));
                            break;
                        case "qq":
                            student.setQq(fi.getString("utf-8"));
                            break;
                        //...
                    }
                } else {
                    if (fi.getSize() < 1)
                        continue;//是文件域，没有选。
                    writer.println("文件域的名字：" + fi.getFieldName() + "，文件名:" + fi.getName());
                    //byte[] bytes = fi.get();
                    //application
                    //web目录下的images目录
                    String images = req.getServletContext().getRealPath("images");
                    File temp = new File(images);
                    if (!temp.exists())
                        temp.mkdirs();
                    //文件名重复的问题。
                    //保持原文件的后缀
                    String oldName = fi.getName();
                    String houzhui = oldName.substring(oldName.lastIndexOf('.'));
                    String newName = UUID.randomUUID() + houzhui;//
                    fi.write(new File(temp, newName));
                    student.setIdcardpic(newName);//!!!!!!!!!!
                    //存储的就是newName
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

//        InputStream inputStream = req.getInputStream();
//        int ch=-1;
//        while ((ch=inputStream.read())!=-1){
//            writer.write((char)ch);
//        }
//        writer.print("<h1>姓名:"+name+"</h1>");
//        writer.print("<h1>qq:"+qq+"</h1>");
//        writer.print("<h1>wechat:"+wec+"</h1>");
//        writer.print("<h1>file:"+file+"</h1>");
        writer.print("</body></html>");
    }
}
